package algorithms.sort;

public class InsertSorter {
	public void sort(Integer[] array, int from, int len) {
		Integer temp = 0;
		for (int i = from + 1; i < len; i++) {
			temp = array[i];
			int j = i;
			for (; j > from; j--) {
				if (temp.compareTo(array[j - 1]) < 0) {
					array[j] = array[j - 1];
				} else {
					break;
				}
			}
			array[j] = temp;
		}
	}
	
	public void sortInJDK(Integer[] array, int offset, int len) {
		for(int i = offset; i < len; i ++) {
			for(int j = i; j > offset && array[j] < array[j - 1]; j --) {
				swap(array, j, j - 1);
			}
		}
	}
	
	private static void swap(Integer[] array, int front, int back) {
		int temp = array[front];
		array[front] = array[back];
		array[back] = temp;
	}

	public static void main(String[] args) {
		Integer[] array = { 47, 23, 58, 16, 24, 10, 75 };
		InsertSorter s = new InsertSorter();
		s.sortInJDK(array, 0, array.length);
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
	}
}
